package com.hougong.im.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Resource(name="redisTemplate")
    private ValueOperations<String, String> valueOperations;
    @Resource(name="redisTemplate")
    private HashOperations<String, Long, Long> hashOperations;
    @Resource(name="redisTemplate")
    private ListOperations<String, Object> listOperations;
    @Resource(name="redisTemplate")
    private SetOperations<String, Object> setOperations;
    @Resource(name="redisTemplate")
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;

    public void set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public <T>T getNotRefreshExpire(String key,Class<T>clazz){
        String value = valueOperations.get(key);
        return value == null ? null : fromJson(value, clazz);
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public Boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }

    public Set<String> getKeys(String pattern){
        return redisTemplate.keys(pattern);
    }

    public void addList(String key,Object object,long expire){
        listOperations.rightPush(key,object);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void addList(String ket,Object object){
        addList(ket,object,NOT_EXPIRE);
    }

    public <T> List<T>  getList(String key){
        List<T> list = (List<T>) listOperations.range(key,0,-1);
        return list;
    }

    /**
     * 获取并删除 list
     * @param key
     * @param <T>
     * @return
     */
    public <T> List<T> popList(String key){
        List<T> list = (List<T>) listOperations.range(key,0,-1);
        if(list!=null && list.size()!=0){
            redisTemplate.delete(key);
        }
        return list;
    }

    public void mapPut(String key,Long filed,Long value){
        hashOperations.put(key, filed,value);
    }

    public Set<Long> mapGetKeys(String key){
        Set<Long> keys = hashOperations.keys(key);
        return keys;
    }

    public Map<Long, Long> mapGetEntries(String key){
        return hashOperations.entries(key);
    }
    public void addSet(String key,Object... object){
        setOperations.add(key,object);
        redisTemplate.expire(key, DEFAULT_EXPIRE, TimeUnit.SECONDS);
    }

    public  <T> Set<T> popSet(String key){
        Set<T> members = (Set<T>) setOperations.members(key);
        if(members!=null && members.size()>0){
            redisTemplate.delete(key);
        }
        return  members;
    }

    public  <T> Set<T> getSet(String key){
        Set<T> members = (Set<T>) setOperations.members(key);
        if(members!=null  && members.size()>0){
            redisTemplate.expire(key, DEFAULT_EXPIRE, TimeUnit.SECONDS);
        }
        return  members;
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return JSON.parseObject(json, clazz);
    }
}